package com.fangcun.controller.admin.old;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fangcun.bean.sys.PageBean;
import com.fangcun.bean.sys.ResponseMsg;
import com.fangcun.common.canstant.ErrCode;
import com.fangcun.common.exception.ProException;
import com.fangcun.common.util.PublicMethod;
import com.fangcun.common.util.apiversion.ApiVersion;
import com.fangcun.common.util.base.BaseAdminController;
import com.fangcun.entity.GoodsCategory;
import com.fangcun.service.admin.GoodsCategoryService;

@RestController
@RequestMapping("/admin/{version}/auth/goods/category")
public class GoodsCategoryController extends BaseAdminController{
	
	private static Logger LOG = LoggerFactory.getLogger(GoodsCategoryController.class);
	
	@Autowired
	private GoodsCategoryService goodsCategoryService;
	
	
	@ApiVersion(1.0)
	@RequestMapping("list")
	private ResponseMsg list(@RequestBody PageBean<GoodsCategory> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			request.getParam().setTeahouseId(getTeahouseId());
			msg.setData(goodsCategoryService.list(request));
		} catch (ProException e) {
			LOG.error("-商品分类列表出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--商品分类列表异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("detail")
	private ResponseMsg detail(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(goodsCategoryService.detail(id));
		} catch (ProException e) {
			LOG.error("-商品分类详情出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--商品分类详情异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("add")
	private ResponseMsg add(@RequestBody GoodsCategory request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goodsCategoryService.add(request);
		} catch (ProException e) {
			LOG.error("-增加商品分类出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--增加商品分类异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	@ApiVersion(1.0)
	@RequestMapping("update")
	private ResponseMsg update(@RequestBody GoodsCategory request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goodsCategoryService.update(request);
		} catch (ProException e) {
			LOG.error("-修改商品分类出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--修改商品分类异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	@ApiVersion(1.0)
	@RequestMapping("delete")
	private ResponseMsg delete(Long id) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			goodsCategoryService.delete(id);
		} catch (ProException e) {
			LOG.error("-删除商品分类出错->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("--删除商品分类异常->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
